//
//  YFSegmentView.m
//  project
//
//  Created by yefei on 2019/5/8.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFSegmentView.h"

@interface YFSegmentView ()

@property (weak, nonatomic) IBOutlet UIView *buttonBackgroundView;

@property (weak, nonatomic) IBOutlet UIView *shadowView;

@property (nonatomic, strong) CALayer *underLine;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UIView *lineView2;
@end

@implementation YFSegmentView

+ (YFSegmentView *)segmentView {
    YFSegmentView *view = [[UINib nibWithNibName:@"YFSegmentView" bundle:nil] instantiateWithOwner:nil options:nil].firstObject;
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 54);
    return view;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    
//    self.shadowView.layer.cornerRadius = 5;
//    self.shadowView.layer.shadowColor = UIColor.blackColor.CGColor;
//    self.shadowView.layer.shadowOffset = CGSizeMake(0, 0);
//    self.shadowView.layer.shadowRadius = 5;
//    self.shadowView.layer.shadowOpacity = 0.15;
//    dispatch_async(dispatch_get_main_queue(), ^{
//        UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.shadowView.bounds];
//        self.shadowView.layer.shadowPath = path.CGPath;
//    });
    [self.lineView rounded:UIRectCornerAllCorners radius:2];
    [self.lineView2 rounded:UIRectCornerAllCorners radius:2];
    
    [self.buttonBackgroundView rounded:(UIRectCornerAllCorners) radius:5];
    
    [self.buttonBackgroundView.layer insertSublayer:self.underLine atIndex:0];
    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        self.underLine.bounds = CGRectMake(0, 0, 40, 4);
//        self.underLine.position = CGPointMake(self.profileButton.center.x, 40);
//
//        self.profilePoint = self.profileButton.center;
//        self.dongtaiPoint = self.dongtaiButton.center;
//    });
    
    @weakify(self)
    [[self.profileButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.profileButton setTitleColor:UIColor.blackColor forState:(UIControlStateNormal)];
        [self.dongtaiButton setTitleColor:UIColor.grayColor forState:(UIControlStateNormal)];
//        [self animateWithlayer:self.underLine endPosition:CGPointMake(x.center.x, 40)];
        [self animateWithBtn:x];
        if (self.buttonClickAction) {
            self.buttonClickAction(0);
        }
    }];
    [[self.dongtaiButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.dongtaiButton setTitleColor:UIColor.blackColor forState:(UIControlStateNormal)];
        [self.profileButton setTitleColor:UIColor.grayColor forState:(UIControlStateNormal)];
//        [self animateWithlayer:self.underLine endPosition:CGPointMake(x.center.x, 40)];
        [self animateWithBtn:x];
        if (self.buttonClickAction) {
            self.buttonClickAction(1);
        }
    }];
    
}

#pragma mark - 动画
- (void)animateWithlayer:(CALayer *)layer endPosition:(CGPoint)endPoint {
    [layer removeAllAnimations];
    layer.position = endPoint;
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"position"];
    ani.duration = 0.5;
    ani.fromValue = [NSValue valueWithCGPoint:layer.position];
    ani.beginTime = CACurrentMediaTime();
    ani.removedOnCompletion = YES;
    [layer addAnimation:ani forKey:@"ButtonPositinAnimation"];
}


-(void)animateWithBtn:(UIButton *)btn {
    @weakify(self)
    [UIView animateWithDuration:0.1 animations:^{
        @strongify(self)
        switch (btn.tag) {
            case 100:
            {
                self.lineView.hidden =NO;
                self.lineView2.hidden = YES;
            }
                break;
            default:
            {
                self.lineView.hidden =YES;
                self.lineView2.hidden = NO;
            }
                break;
        }
    }];
}


#pragma mark - 动画
- (void)controlAnimateWithTimeOffset:(CGFloat)offset endPoint:(CGPoint)endPoint {
    
    if (![self.underLine animationForKey:@"ButtonPositinAnimation"]) {
        [self addControlAnimateWithEndPoint:endPoint];
    }
    self.underLine.timeOffset = offset;
    self.profileButton.layer.timeOffset = offset;
    self.dongtaiButton.layer.timeOffset = offset;
}

- (void)addControlAnimateWithEndPoint:(CGPoint)endPoint {
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"position"];
    ani.duration = 1.0;
    ani.fromValue = [NSValue valueWithCGPoint:self.underLine.position];
    ani.toValue = [NSValue valueWithCGPoint:endPoint];
    [self.underLine addAnimation:ani forKey:@"ScrollViewPositionAnimation"];
    self.underLine.speed = 0.0;
    ani.removedOnCompletion = YES;
}


//- (CALayer *)underLine {
//    if (!_underLine) {
//        _underLine = [[CALayer alloc] init];
//        _underLine.backgroundColor = UIColor.redColor.CGColor;
//    }
//    return _underLine;
//}

@end
